//
//  GuidingImagesVC.m
//  MyNewProject
//
//  Created by 王春辉 on 2020/2/9.
//  Copyright © 2020 sun. All rights reserved.
//

#import "GuidingImagesVC.h"

@interface GuidingImagesVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIButton *startButton;

@property (nonatomic, strong) NSArray <UIImage *>*images;
@end

@implementation GuidingImagesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view);
        make.height.mas_equalTo(self.view);
    }];
    
    [self.startButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-SKBottomSafe_Height - 22.5);
        make.centerX.mas_offset(0);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(165.0 / 375.0);
        make.height.mas_equalTo(self.startButton.mas_width).multipliedBy(36.0 / 165);
    }];
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_offset(0);
        make.bottom.mas_equalTo(self.startButton.mas_top).mas_offset(-4);
        make.left.right.mas_offset(0);
    }];
    
    self.pageControl.numberOfPages = self.images.count;
    for (int i = 0; i < self.images.count; i++) {
        UIImage *image = self.images[i];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [self.scrollView addSubview:imageView];
//        imageView.contentMode = UIViewContentModeScaleAspectFit;
        if (i == (self.images.count - 1)) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(i * (self.view.width));
                make.top.bottom.mas_offset(0);
                make.width.mas_equalTo(self.view);
                make.width.mas_equalTo(self.scrollView);
                make.height.mas_equalTo(self.view);
                make.height.mas_equalTo(self.scrollView);
                make.right.mas_offset(0);
            }];
        }else {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_offset(i * (self.view.width));
                make.top.bottom.mas_offset(0);
                make.width.mas_equalTo(self.view);
                make.width.mas_equalTo(self.scrollView);
                make.height.mas_equalTo(self.view);
                make.height.mas_equalTo(self.scrollView);
            }];
        }
    }
}


#pragma mark -- mark 懒加载
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
        _scrollView.pagingEnabled = true;
        _scrollView.bounces = false;
        _scrollView.delegate = self;
        _scrollView.showsVerticalScrollIndicator = false;
        _scrollView.showsHorizontalScrollIndicator = false;
        [self.view addSubview:_scrollView];
    }
    return _scrollView;
}
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
        [_pageControl setValue:[UIImage imageNamed:@"shop_recomend_pagecontrol_current_img"] forKeyPath:@"_pageImage"];
        [_pageControl setValue:[UIImage imageNamed:@"shop_recomend_pagecontrol_default_img"] forKeyPath:@"_currentPageImage"];
        [self.view addSubview:_pageControl];
    }
    return _pageControl;
}
- (UIButton *)startButton {
    if (!_startButton) {
        self.startButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_startButton setTitle:@"立即体验" forState:UIControlStateNormal];
        [_startButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _startButton.titleLabel.font = SKFONT_BOLD_SIZE(14);
        _startButton.layer.masksToBounds = true;
        _startButton.backgroundColor = SKColorWithHex(0xFF4B2B);
        _startButton.layer.cornerRadius = 18.0;
        _startButton.hidden = true;
        [_startButton addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_startButton];
    }
    return _startButton;
}
- (NSArray<UIImage *> *)images {
    if (!_images) {
        NSMutableArray <UIImage *>*images = [NSMutableArray array];
        for (int i = 1; i <= 4; i++) {
            NSString *pathName = [NSString stringWithFormat:@"guiding_%d", i];
            NSString *path = [[NSBundle mainBundle] pathForResource:pathName ofType:@"jpg"];
            UIImage *image = [UIImage imageWithContentsOfFile:path];
//            UIImage *image = [UIImage imageNamed:pathName];
            [images addObject:image];
        }
        _images = images;
    }
    return _images;
}
#pragma mark -- mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger page = scrollView.contentOffset.x / scrollView.width;
    [self.pageControl setCurrentPage:page];
    if (page == (self.images.count - 1)) {
        self.startButton.hidden = false;
    }else {
        self.startButton.hidden = true;
    }
}

#pragma mark -- mark EVENT
- (void)buttonClick:(UIButton *)sender {
    if (self.CompleteHandler) {
        self.CompleteHandler();
    }
}
@end
